<template>
  <div class="view-container">
    <el-form :model="queryParam">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="字典名称" prop="dictName">
            <el-input v-model="queryParam.dictName" placeholder="字典名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="字典类型" prop="dictType">
            <el-input v-model="queryParam.dictType" placeholder="字典类型"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetParams">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <nebula-table :columns="columns" :data="tableData" header-row-class-name="header-row-class" size="default" border
                  :total="rowsTotal" :default-page-size="10" show-overflow-tooltip use-tool-bar @change="changePage">
      <template #head-toolbar>
        <el-button type="primary" @click="handleCreate" size="small">新增</el-button>
      </template>
      <template #dictType="scope">
        <router-link :to=genDetailUrl(scope.row) class="table-link">
          {{ scope.row.dictType }}
        </router-link>
      </template>
      <template #operation="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </nebula-table>
    <el-dialog v-model="isShow" title="新增字典">
      <div class="view-container">
        <el-form :model="createParam" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="createParam.dictName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="createParam.dictType"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="createParam.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="createCancel">取消</el-button>
        <el-button type="primary" @click="createSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {createOrEditDictType, deleteDictType, getDictTableData} from "@/api/sysDict.js";
import NebulaTable from "@/components/NebulaTable.vue";
import {ElMessageBox, ElNotification} from "element-plus";
// import {layer} from "@layui/layui-vue";

defineOptions({
  name: 'sysDict'

})
const queryParam = ref({
  dictName: null,
  dictType: null,
  pageNum: 1,
  pageSize: 10,
})

const createParam = ref({
  dictName: null,
  dictType: null,
  remark: null,
})
const isShow = ref(false)

const columns = ref([
  {title: "字典名称", key: "dictName", align: "center"},
  {title: "字典类型", key: "dictType", align: "center", customSlot: 'dictType'},
  {title: "创建时间", key: "createTime", align: "center"},
  {title: "备注", key: "remark", align: "center"},
  {title: "操作", align: 'center', customSlot: 'operation'}
])
const tableData = ref([])
const rowsTotal = ref(0)

const genDetailUrl = (row) => {
  return "/system/dict/detail/" + row.id;
}

const resetParams = () => {
  for (let key in queryParam.value) {
    queryParam.value[key] = null;
  }
}

const handleQuery = () => {
  getDictTableData(queryParam.value).then(res => {
    tableData.value = res.data.rows
    rowsTotal.value = res.data.total
  })
}

const changePage = (currentPage,pageSize) => {
  queryParam.value.pageNum = currentPage
  queryParam.value.pageSize = pageSize
  handleQuery()
}

const handleCreate = () => {
  isShow.value = true
}

const handleEdit = (row) => {
  handleCreate()
  createParam.value = {...row}
}

const createSubmit = () => {
  createOrEditDictType(createParam.value).then(res => {
    if (res.code === 200) {
      isShow.value = false
      ElNotification({
        title: '操作成功',
        type: 'success',
      })
      handleQuery()
    } else {
      ElNotification({
        title: '创建失败',
        message: res.msg,
        type: 'error',
      })
    }
  })
}

const createCancel = () => {
  isShow.value = false
  for (let key in createParam.value) {
    createParam.value[key] = null;
  }
}

function handleDelete(row) {
  const confirmText = `确定要删除字典类型为【${row.dictType}】及其所有字典值吗?`
  ElMessageBox.confirm(
      confirmText,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    let param = {id: row.id}
    deleteDictType(param).then(res => {
      if (res.code === 200) {
        ElNotification({
          title: '操作成功',
          type: 'success',
        })
      } else {
        ElNotification({
          title: '操作失败',
          message: res.msg,
          type: 'error',
        })
      }
      handleQuery()
    })
  })
}

onMounted(() => {
  handleQuery()
})
</script>
